using UnityEngine;
using System.Collections;
using System;

public class GameInfos
{
	#region SINGLETON
	private static GameInfos instance;
	public static GameInfos Instance
	{
		get
		{ 
			if(null == instance)
			{
				instance = new GameInfos();
			}
			return instance;
		}
	}
	public static void Kill()
	{
		instance = null;
	}
	#endregion
	
	private const string PLAYER_NAME_UID = "PlayerName";
	private const string BEST_SCORE_UID = "BestScore";
	private const string BEST_TIME_UID = "BestTime";
	private const string BEST_LEVEL_UID = "BestLevel";
	
	private string playerName;
	private int score;
	private float time;
	private int level;
	private int bestScore;
	private float bestTime;
	private int bestLevel;
	
	public string PlayerName{
		get{ return playerName; }
		set{ playerName = value; }
	}
	public int Score{
		get{ return score; }
		set
		{ 
			if(value > BestScore)
			{
				BestScore = value;
			}
			score = value;
		}
	}
	public float TimeScore{
		get{ return time; }
		set
		{ 
			if(value > BestTime)
			{
				BestTime = value;
			}
			time = value; 
		}
	}
	public int Level{
		get{ return level; }
		set
		{ 
			if(value > BestLevel)
			{
				BestLevel = value;
			}
			level = value; 
		}
	}
	public int BestScore{
		get{ return bestScore; }
		set{ bestScore = value; }
	}
	public float BestTime{
		get{ return bestTime; }
		set{ bestTime = value; }
	}
	public int BestLevel{
		get{ return bestLevel; }
		set{ bestLevel = value; }
	}
	public GameInfos()
	{
		//PlayerName = GetData<string>(PLAYER_NAME_UID);
		//BestScore = GetData<int>(BEST_SCORE_UID);
		//BestTime = GetData<float>(BEST_TIME_UID);
		//BestLevel = GetData<int>(BEST_LEVEL_UID);
	}
	public void AddScore(int scoreToAdd)
	{
		Score += scoreToAdd;
	}
	public void AddScoreTime(float timeScoreToAdd)
	{
		TimeScore += timeScoreToAdd;
	}
	public void Save()
	{
		PlayerPrefs.SetString(PLAYER_NAME_UID, PlayerName);
		PlayerPrefs.SetInt(BEST_SCORE_UID, BestScore);
		PlayerPrefs.SetFloat(BEST_TIME_UID, BestTime);
		PlayerPrefs.SetInt(BEST_LEVEL_UID, BestLevel);
		PlayerPrefs.Save();
	}
	
	public void GetDataInt(string uid, ref int val)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			val = GetDataInt(uid);			
		}
		else
		{
			PlayerPrefs.SetInt(uid, 0);	
		}			
	}	
	public int GetDataInt(string uid)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			return PlayerPrefs.GetInt(uid);			
		}
		else
		{
			PlayerPrefs.SetInt(uid, 0);
		}
		return 0;
	}
	public void GetDataFloat(string uid, ref float val)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			val = GetDataFloat(uid);			
		}
		else
		{
			PlayerPrefs.SetFloat(uid, 0.0f);	
		}
	}
	public float GetDataFloat(string uid)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			return PlayerPrefs.GetFloat(uid);			
		}
		else
		{
			PlayerPrefs.SetFloat(uid, 0.0f);
		}
		return 0.0f;
	}
	public void GetDataString(string uid, ref string val)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			val = GetDataString(uid);			
		}
		else
		{
			PlayerPrefs.SetString(uid, "");	
		}			
	}
	public string GetDataString(string uid)
	{
		if (PlayerPrefs.HasKey(uid))
		{
			return PlayerPrefs.GetString(uid);			
		}
		else
		{
			PlayerPrefs.SetString(uid, "");
		}
		return "";
	}
}
